package threadPool;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;

/**
 * @author: 李德才
 * @description:
 * @create: 2020-12-20 14:39
 **/
public class T11_ScheduledPool {

    /**
     * 有计划的线程池
     * ========> Executors.newScheduledThreadPool(10);
     * -----------------------------------------------------
     * public ScheduledThreadPoolExecutor(int corePoolSize) {
     * super(corePoolSize,
     * //最大线程数是  Integer.MAX_VALUE,
     * Integer.MAX_VALUE,
     * DEFAULT_KEEPALIVE_MILLIS, MILLISECONDS,
     * // DelayedWorkQueue: 按照时间排序执行，时间越短的优先执行
     * new DelayedWorkQueue());
     * }
     */

    static final ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(10);

    public static void main(String[] args) {

        /**
         * 执行20个任务，每个任务占用线程一秒
         * 线程池只有 10 个线程的位置，剩下的 10 个要等待
         * 一秒之后顺序执行
         *
         */
        for (int i = 0; i < 20; i++) {
            final int j = i;
            scheduledExecutorService.execute(() -> {
                System.err.println(Thread.currentThread().getName() + "=======> " + j);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });

        }
        scheduledExecutorService.shutdown();
    }

}
